﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SurvivalStrategy.WorldData.Colliders;
using SurvivalStrategy.WorldData.ItemComponents;

namespace SurvivalStrategy.WorldData
{
    public class WorldElement
    {  
        public WorldChunk ChunkOwner { get; internal set;}
        public int ID { get; internal set; }
        protected List<ItemComponent> components;
        public int TypeID { get; internal set; } 
        public int TextureID { get; internal set; }
        public Vector3 Position { get; internal set; }
        public Vector3 Rotation { get; internal set; }
        public Vector3 Size { get; internal set; }
        public bool Collidable { get; internal set; }
        public bool Removable { get; internal set; }
        public List<List<WorldElement>> Owners { get; internal set; }

        public WorldElement() { }

        public WorldElement(int textureID, Vector3 position, Vector3 rotation, Vector3 size, bool collidable, Collider collider)
        {
            Owners = new List<List<WorldElement>>();
            components = new List<ItemComponent>();
            this.TextureID = textureID;
            this.Position = position;
            this.Size = size;
            this.Collidable = collidable;
            this.Rotation = rotation;
            this.AddComponent<Collider>(collider);
            this.Removable = false;
        }

        public WorldElement(Vector3 position, Vector3 rotation, int typeID)
        {
            Owners = new List<List<WorldElement>>();
            components = new List<ItemComponent>();
            this.Position = position;
            this.Rotation = rotation;
            this.Size = World.ElementsTypes[typeID].Size;
            this.Collidable = World.ElementsTypes[typeID].Collidable;
            this.AddComponent<Collider>(World.ElementsTypes[typeID].CollideType);
            this.TextureID = World.ElementsTypes[typeID].TextureID;
        }

        public void SetOwner(List<WorldElement> owner)
        {
            this.Owners.Add(owner);
        }

        public void SetChunkOwner(WorldChunk chunkOwner)
        {
            this.ChunkOwner = chunkOwner;
        }

        public bool CheckCollision(Collider other)
        {
            return this.GetComponent<Collider>().CheckCollision(other);
        }

        public void SetElementId(int id)
        {
            this.ID = id;
        }


        public void AddComponent<T>() where T : ItemComponent, new()
        {
            if (this.GetComponent<T>() != null)
                return;
            T t = new T();
            t.SetOwner(this);
            components.Add(t);
        }


        public void AddComponent<T>(T obj) where T : ItemComponent, new()
        {
            if (this.GetComponent<T>() != null)
                return;
            if(obj == null)
                obj = new T();
            T t = obj;
            t.SetOwner(this);
            components.Add(t);
        }

        public T GetComponent<T>() where T : ItemComponent
        {
            foreach (ItemComponent c in components)
                if (c is T)
                    return (T)c;
            return null;
        }
    }
}
